Cufon.replace('h2');
Cufon.replace('h3');
Cufon.replace('h4');
Cufon.replace('h5');
Cufon.replace('h6');
Cufon.replace('#n ul li legend');
Cufon.replace('#teaser-boxes li.b h3', {hover: true});
Cufon.replace('#cloud span');
Cufon.replace('#l ul li a', {hover: true});
Cufon.replace('#n ul li a', {hover: true});

      
function addIEStuff(ele,ele2,id)
{
    if(!id)
      id = "IEShadow";
      
    if(!$.browser.msie)
      return;

    var el = $(ele).find(ele2);
        var pt=0,pl=0,pr=0,pb=0;
               
        try{
        pt = parseInt(el.css('padding-top'));
        } catch(e){}
        
        try{
        pl = parseInt(el.css('padding-left'));
        } catch(e){}
        
        
        try {
        pr = parseInt(el.css('padding-right'));
        } catch(e){}
        
        
        try{
        pb = parseInt(el.css('padding-bottom'));
        } catch(e){}
    
    var h = $(ele).find(ele2).height() +pt +pb; //+ add;
    var w = $(ele).find(ele2).width() +pl +pr; //+ add;
    $('#'+id).remove();
    $(ele).append($('<div id="'+id+'"></div>').html('&nbsp;').css({'height':h+'px', 'width':w+'px'}));
}

function update_teaser_text(text)
{
  $('#cloud-desc').html(text).show();
  Cufon.replace('#cloud-desc h2');
  Cufon.now();
  addIEStuff($('#h'),'#cloud-desc',"IECloudShadow");
}

var ugly_timeOut = null;
var killed = false;
function kill_ugly_animation()
{
      killed = true;
    
      clearTimeout(ugly_timeOut);
      ugly_timeOut = null;
      
      //rewind!
      $('#typo_slider ul li').fadeTo(0,1);
      $('#typo_slider ul li').removeAttr('style');
      next = $($('#typo_slider ul li')[0]);
}

function start_beeing_ugly()
{
  
  clearTimeout(ugly_timeOut);
  ugly_timeOut = null;
  
  if($('#typo_slider').hasClass('popup-show'))
  {
      return;
  }
  
  
  $('#typo_slider ul .vis').fadeTo(200, 0, function(){
    
    $(this).removeClass('vis').hide();
    
    var next = $(this).next('li');
        
    if(killed)
    {
      //console.log('killed!');
      if(next.length && !next.hasClass('link'))
      {
        next.addClass('vis');
      }
      else
      {
        $($('#typo_slider ul li')[0]).addClass('vis');
      } 
        
      kill_ugly_animation();
      killed = false;
      
      //console.log('killed = false');
      
      return;
    }
    
    if(next.length && !next.hasClass('link'))
    {
        next.fadeTo(0,0).addClass('vis')
        next.fadeTo(200,1, function(){$(this).removeAttr('style')});
        
        if(ugly_timeOut == null)  
          ugly_timeOut = setTimeout(start_beeing_ugly, 2000);
          
    } 
    else 
    {
      //rewind!
      $('#typo_slider ul .vis').removeClass('vis').removeAttr('style');
      $('#typo_slider ul li').removeAttr('style');
      
      next = $($('#typo_slider ul li')[0]);
      next.fadeTo(0,0).addClass('vis')
      next.fadeTo(200,1, function(){$(this).removeAttr('style')});
      
      if(ugly_timeOut == null)  
        ugly_timeOut = setTimeout(start_beeing_ugly, 2000);
      
    }
  })
  
}


$(document).ready(function() {
(function(){
      try{
      if($('#h #cloud-desc').length > 0) {
        addIEStuff($('#h'),'#cloud-desc',"IECloudShadow");
      }
      }catch(e){}


  if($('#typo_slider').length > 0)
    {
       leUL =  $('#typo_slider ul');
       ugly_timeOut = setTimeout(start_beeing_ugly, 2000);
    }

    var special = jQuery.event.special,
        uid1 = 'D' + (+new Date()),
        uid2 = 'D' + (+new Date() + 1);
 
    special.scrollstart = {
        setup: function() {
 
            var timer,
                handler =  function(evt) {
 
                    var _self = this,
                        _args = arguments;
 
                    if (timer) {
                        clearTimeout(timer);
                    } else {
                        evt.type = 'scrollstart';
                        jQuery.event.handle.apply(_self, _args);
                    }
 
                    timer = setTimeout( function(){
                        timer = null;
                    }, special.scrollstop.latency);
 
                };
 
            jQuery(this).bind('scroll', handler).data(uid1, handler);
 
        },
        teardown: function(){
            jQuery(this).unbind( 'scroll', jQuery(this).data(uid1) );
        }
    };
 

$('.click-to-open').click(function(e) {
e.preventDefault();
var click = $(this).closest('.click-popup')
var height = click.height();
click.css('height', height + "px");
click.addClass('popup-show');
addIEStuff(click, '.popup-content');
click.find('.popup-content').mouseleave(function(){ if($(this).hasClass('popup-cclose')) return; click.removeClass('popup-show'); $('#IEShadow').remove(); });
})

$('.popup-close').click(function(e){
  e.preventDefault();
  
  $(this).parent().parent().removeClass('popup-show'); 
  
  $('#IEShadow').remove();

})


    special.scrollstop = {
        latency: 300,
        setup: function() {
 
            var timer,
                    handler = function(evt) {
 
                    var _self = this,
                        _args = arguments;
 
                    if (timer) {
                        clearTimeout(timer);
                    }
 
                    timer = setTimeout( function(){
 
                        timer = null;
                        evt.type = 'scrollstop';
                        jQuery.event.handle.apply(_self, _args);
 
                    }, special.scrollstop.latency);
 
                };
 
            jQuery(this).bind('scroll', handler).data(uid2, handler);
 
        },
        teardown: function() {
            jQuery(this).unbind( 'scroll', jQuery(this).data(uid2) );
        }
    };
 
})();




  //fallback
  var words = $('#cloud span').length;
  var split = 360 / words;
  
  
  var i = 1;
  var height = $('#cloud').height() / 2;
  var width = height;// $('#cloud').width() / 4;
  
  
  try{
    $('#red-dot').css({top:height-55, left:80+height});
    $('#cloud span').each(function(o,e){
    
     if($(e).width() > 200)
     {
       $(e).css('width','200px');
     }
     
     $(e).css({'top' :height + height*Math.sin(i*split*Math.PI/180), 
              'left': 150 + width + width *Math.cos(i*split*Math.PI/180) - (Math.cos(i*split*Math.PI/180) < 0 ? $(e).width() : 0),
              'text-align':'center'
              });
            
            i+=1;
    });
    
    
  }catch(e){}

  //rueckrufservice
  
  $('#rueckrufservice input').focus(
    function(){
      if(this.value == $(this).attr('title'))
      {
        this.value='';
      }
    });
  $('#rueckrufservice input').blur(
    function(){
      if(this.value=='')
      {
        this.value = $(this).attr('title');
      }
    }
  );


  $('#l .popup, #n .popup').hover(function(){
  $(this).addClass('popup-show');
    
    addIEStuff(this,'ul');  

  }, function(){
    $('#IEShadow').remove();
    $(this).removeClass('popup-show');
    Cufon.refresh('#n ul li a');
  });
  
  $('#r .popup, #teaser-boxes .popup').hover(function(){
  
    if(this.id == 'typo_slider')
      kill_ugly_animation();
    
    if(this.tagName == 'DIV')
    {
      var height = $(this).height();
      
      if($.browser.msie && $.browser.version == "6.0")
      {
        height += 2;//padding and border as defined in css
      }
      
      $(this).css('height',height);
    }
    
    
    $(this).addClass('popup-show');
    
    addIEStuff(this, '.popup-content');
    
    if($.browser.msie && $.browser.version == "6.0" && $('#r #IEShadow'))
    {
      var ele = $('#r #IEShadow');
      ele.css('height', parseInt(ele.height()) - 8 + "px");
    }
    
    
 }, function(){
    $('#IEShadow').remove();
    $(this).removeClass('popup-show');

    if(this.id == 'typo_slider' && ugly_timeOut == null)
    {
     killed = false;
     setTimeout(start_beeing_ugly,2000);
     }
  });


  var win  = $(window);
  win.bind('scrollstop',function() {
      if(win.scrollTop() >= 830) 
        $('#l,#r').animate({'padding-top':win.scrollTop() -430 });
      else {
       // $('#l,#r').css('padding-top', 0);
       $('#l,#r').animate({'padding-top':0});
      }
  });
});